package com.example.gulimallware.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.example.gulimallware.vo.MergeVo;
import com.example.gulimallware.vo.PurchaseDoneVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.example.gulimallware.entity.WmsPurchaseEntity;
import com.example.gulimallware.service.WmsPurchaseService;
import com.example.common.utils.PageUtils;
import com.example.common.utils.R;



/**
 * 采购信息
 *
 * @author zzl
 * @email zzl@gmail.com
 * @date 2025-10-27 08:23:54
 */
@RestController
@RequestMapping("/purchase")
public class WmsPurchaseController {
    private static final Logger log = LoggerFactory.getLogger(WmsPurchaseController.class);
    @Autowired
    private WmsPurchaseService wmsPurchaseService;

    /**
     * 完成采购单
     */
    @PostMapping("/done")
    public R done(@RequestBody @Validated PurchaseDoneVo purchaseDoneVo){
        log.info("完成采购单：{}",purchaseDoneVo);
        return wmsPurchaseService.done(purchaseDoneVo);
    }

    /**
     * 领取采购单
     */
    @PostMapping("/received")
    public R received(@RequestBody ArrayList<Long> ids){
        return wmsPurchaseService.received(ids);
    }

      /**
     * 查询未领取的采购单
     */
    @GetMapping("/unreceive/list")
    public R unreceiveList(){
        return wmsPurchaseService.queryPageUnreceivePurchase();
    }


    /**
     * 合并采购需求
     */
    @PostMapping("/merge")
    public R merge(@RequestBody MergeVo mergeVo){
        return wmsPurchaseService.merge(mergeVo);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wmsPurchaseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		WmsPurchaseEntity wmsPurchase = wmsPurchaseService.getById(id);

        return R.ok().put("purchase", wmsPurchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody WmsPurchaseEntity wmsPurchase){
        wmsPurchase.setCreateTime(new Date());
        wmsPurchase.setUpdateTime(new Date());
		wmsPurchaseService.save(wmsPurchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody WmsPurchaseEntity wmsPurchase){
		wmsPurchaseService.updateById(wmsPurchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		wmsPurchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
